package com.arges.dao;

import com.arges.pojo.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDao {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * hibernate 框架 添加是返回的是主键
     * @return
     */
    public Integer save(Book book){
        Session session = sessionFactory.getCurrentSession();
        Integer id = (Integer) session.save(book);
        return id;
    }
    /**
     * hibernate 修改
     * @return
     */
    public void update(Book book){
        // 获取 session
        Session session =sessionFactory.getCurrentSession();
        // 执行修改操作
        session.update(book);
    }
    /**
     * hibernate 删除
     * @return
     */
    public void delete(Book book){
        // 获取 session
        Session session = sessionFactory.getCurrentSession();

        // 执行删除操作
        session.delete(book);
    }
    /**
     * hibernate 根据id查询
     * @return
     */
    public Book selectById(Integer id){
        // 获取 session
        Session session = sessionFactory.getCurrentSession();

        // 执行根据id查询操作
        Book book = session.get(Book.class, id);

        return book;
    }

    /**
     * hibernate 查询所有
     * @return
     */
    public List<Book> findAll(String hql) {
        // 获取 session
        Session session = sessionFactory.getCurrentSession();
        // 执行查询所有方法
        List<Book> list = session.createQuery(hql).list();
        return list;
    }


}
